﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ReallyReallyReallySimpleRoguelike.Entities;
using System.Threading;
using ReallyReallyReallySimpleRoguelike.Managers;
using System.Drawing;

namespace ReallyReallyReallySimpleRoguelike.Helpers
{
    public class DungeonVisualizer
    {
        int StartPointX;
        int StartPointY;
        public Panel TopPanel;
        public Panel BottomPanel;

        //for drawing dungeon
        public string[,] strings;
        public ConsoleColor[,] colors;
        
        //for preventing redrawing
        public string[,] oldStrings;
        public ConsoleColor[,] oldColors;

        public DungeonVisualizer()
        {
            StartPointX = Console.WindowWidth / 2 - Constants.DungeonWidth / 2;
            StartPointY = Console.WindowHeight / 2 - Constants.DungeonHeight / 2;
            
            TopPanel = new Panel(new Point(StartPointX - 18,StartPointY-3),56);
            BottomPanel = new Panel(new Point(StartPointX - 18, StartPointY +24), 56);
            
            strings = new string[Constants.ConsoleWidth, Constants.DungeonHeight];
            colors = new ConsoleColor[Constants.ConsoleWidth, Constants.DungeonHeight];
            oldStrings = new string[Constants.ConsoleWidth, Constants.DungeonHeight];
            oldColors = new ConsoleColor[Constants.ConsoleWidth, Constants.DungeonHeight];
        }

        public void Visualize(DungeonLevel dungeonLevel, bool drawPanel)
        {
            //Console.Clear();
            //Repeating this makes sure the console has not been resized.
            Console.SetWindowSize(Constants.ConsoleWidth, Constants.ConsoleHeight);
            Console.SetBufferSize(Constants.ConsoleWidth, Constants.ConsoleHeight);
            
            
            TopPanel.Write(dungeonLevel.ToString(), ConsoleColor.White);
            
            if (drawPanel)
            DrawPanels(StartPointX, StartPointY);
            
            
            Draw(dungeonLevel, StartPointX, StartPointY+1);

            // Display HUD            
            DisplayHUD(StartPointX, StartPointY+1);
        }

        private void DrawPanels(int StartPointX, int StartPointY)
        {
            //Draw top PanelWrite title
            //top
            Console.ForegroundColor = ConsoleColor.White;
            Console.SetCursorPosition(StartPointX - 20, StartPointY - 5);
            for (int i = 0; i < 60; i++)
                Console.Write("+");
            Console.SetCursorPosition(StartPointX - 20, StartPointY - 1);
            for (int i = 0; i < 60; i++)
                Console.Write("+");
            //bottom
            Console.SetCursorPosition(StartPointX - 20, StartPointY + 22);
            for (int i = 0; i < 60; i++)
                Console.Write("+");
            Console.SetCursorPosition(StartPointX - 20, StartPointY + 26);
            for (int i = 0; i < 60; i++)
                Console.Write("+");
            //left
            for (int i = 0; i < 32; i++)
            {
                Console.SetCursorPosition(StartPointX - 20, StartPointY - 5 + i);
                Console.WriteLine("+");
            }
            for (int i = 0; i < 22; i++)
            {
                Console.SetCursorPosition(StartPointX - 2, StartPointY + i);
                Console.WriteLine("+");
            }
            //right
            for (int i = 0; i < 22; i++)
            {
                Console.SetCursorPosition(StartPointX + 21, StartPointY + i);
                Console.WriteLine("+");
            }
            for (int i = 0; i < 32; i++)
            {
                Console.SetCursorPosition(StartPointX + 39, StartPointY - 5 + i);
                Console.WriteLine("+");
            }
        }

        public void Flash(Tile tile)
        {
            for (int i = 0; i < 10; i++)
            {
                if (tile.Color == ConsoleColor.Red)
                    tile.Color = ConsoleColor.White;
                else
                    tile.Color = ConsoleColor.Red;

                DungeonManager.Instance.ForceRedraw();

                //should be replaced with a timer
                Thread.Sleep(10);
            }
        }

        public void ForceFullVisualization(DungeonLevel dungeonLevel)
        {
            Console.Clear();
            TopPanel.Write("?", ConsoleColor.White);
            oldStrings = new string[Constants.ConsoleWidth, Constants.DungeonHeight];
            oldColors = new ConsoleColor[Constants.DungeonWidth,Constants.DungeonHeight];
            Visualize(dungeonLevel,true);
        }

        private void Draw(DungeonLevel dungeonLevel, int StartPointX, int StartPointY)
        {
            //first draw level to arrays
            for (int i = 0; i < Constants.DungeonHeight; i++)
            {
                for (int j = 0; j < Constants.DungeonWidth; j++)
                {
                    strings[j, i] = dungeonLevel.Tiles[j, i].ImageCharacter;
                    colors[j,i] = dungeonLevel.Tiles[j, i].Color;
                }
            }
            
            //place cursor
            Console.SetCursorPosition(StartPointX, StartPointY);
            
            for (int i = 0; i < Constants.DungeonHeight; i++)
            {
                for (int j = 0; j < Constants.DungeonWidth; j++)
                {
                    Console.ForegroundColor = dungeonLevel.Tiles[j, i].Color;
                    if (strings[j, i] != oldStrings[j,i] || colors[j,i] != oldColors[j,i])
                    {
                        Console.SetCursorPosition(StartPointX + j, StartPointY + i);
                        Console.Write(strings[j, i]);
                    }
                }
                Console.WriteLine();
            }
            // must clone here othewise points to the same memory location.  Notice the cast.
            //Not convinced that the tiles are cloned here
            oldStrings = (string[,])strings.Clone();
            oldColors = (ConsoleColor[,])colors.Clone();
            
        }

        private void DisplayHUD(int StartPointX, int StartPointY)
        {
            if (DungeonManager.Instance.CreatureManager.Player.Inventory.Any<Sword>())
            {
                StringBuilder sb = new StringBuilder("Sword: ");
                
                for (int i = 0; i < DungeonManager.Instance.CreatureManager.Player.Inventory[0].UsesRemaining; i++)
                {
                    sb.Append("*");
                }

                BottomPanel.Write(sb.ToString(), ConsoleColor.White);

            }
            else
            {
                BottomPanel.Clear();
            }

        }
    }
}
